package cn.bob.order.service.impl;

import cn.bob.basic.config.BussinessException;
import cn.bob.basic.utils.PageList;
import cn.bob.order.domain.OrderPetAcquisition;
import cn.bob.order.domain.OrderPetAcquisitionLog;
import cn.bob.order.dto.OrderDto;
import cn.bob.order.mapper.OrderPetAcquisitionLogMapper;
import cn.bob.order.mapper.OrderPetAcquisitionMapper;
import cn.bob.order.service.IOrderPetAcquisitionService;
import cn.bob.basic.service.impl.BaseServiceImpl;
import cn.bob.pet.domain.SearchMasterMsg;
import cn.bob.pet.mapper.SearchMasterMsgMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bob
 * @since 2022-11-01
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {
    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private OrderPetAcquisitionLogMapper orderPetAcquisitionLogMapper;


    @Override
    public void confirm(OrderDto orderDto) {
        //参数校验
        if (orderDto.getMoney()==null || orderDto.getPayType()==null){
        throw new BussinessException("参数不能为空！！");
        }
        //修改状态
        //修改实际交易金额
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.findById(orderDto.getId());
        orderPetAcquisition.setPaytype(orderDto.getPayType());
        orderPetAcquisition.setPrice(orderDto.getMoney());
        orderPetAcquisition.setPetId(1L);
        orderPetAcquisition.setState(1);
        orderPetAcquisitionMapper.update(orderPetAcquisition);
    }

    @Override
    public void cancel(Long id) {
        //参数校验传入的参数不为空
        if (id==null){
            throw new BussinessException("参数不能为空！！");
        }
        //修改shopid为null  状态改为1待处理
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.findById(id);
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.findById(orderPetAcquisition.getSearchMasterMsgId());
        searchMasterMsg.setShopId(null);
        searchMasterMsg.setState(1);
        searchMasterMsgMapper.update(searchMasterMsg);

        //状态改为-1取消订单 todo 生成取消订单日志
        orderPetAcquisition.setState(-1);
        orderPetAcquisitionMapper.update(orderPetAcquisition);

        OrderPetAcquisitionLog orderPetAcquisitionLog = new OrderPetAcquisitionLog();
        orderPetAcquisitionLog.setFailtime(new Date());
        orderPetAcquisitionLog.setOrderId(id);
        orderPetAcquisitionLogMapper.add(orderPetAcquisitionLog);
    }

}
